package com.atlzk.renting.house.controller;

import com.atlzk.common.entity.house.HouseEntity;
import com.atlzk.common.utils.PageUtils;
import com.atlzk.common.utils.R;
import com.atlzk.renting.house.dao.HouseESDao;
import com.atlzk.renting.house.entity.HouseEs;
import com.atlzk.renting.house.service.HouseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author liuzekun
 * @email 2803587841@gmail.com
 * @date 2021-07-16 20:03:44
 */
@RestController
@RequestMapping("house/house")
@CrossOrigin
public class HouseController {
    @Autowired
    private HouseService houseService;

    @Autowired
    private HouseESDao houseESDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("house:house:list")
    public R list(@RequestParam Map <String, Object> params) {
        PageUtils page = houseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("house:house:info")
    public R info(@PathVariable("id") Long id) {
        HouseEntity house = houseService.getById(id);

        return R.ok().put("house", house);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("house:house:save")
    public R save(@RequestBody HouseEntity house) {
        houseService.save(house);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("house:house:update")
    public R update(@RequestBody HouseEntity house) {
        houseService.updateById(house);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("house:house:delete")
    public R delete(@RequestBody Long[] ids) {
        houseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 分页列表
     *
     * @param houseEntity
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/house_list")
    public IPage houseList(@RequestBody HouseEntity houseEntity,
                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        return houseService.houseList(houseEntity, page, size);
    }

    /**
     * es 查询
     */
    @RequestMapping("/es_list")
    public void esList() {

    }

    /**
     * es 添加
     */
    @RequestMapping("/es_save")
    public void esSave() {
//        HouseEs houseEs = new HouseEs(null, 1, 1, 1, "花果山", 1, new BigDecimal(200), 1, 1, 1, 1, 1, new Double(1), 1, 1, 1, "{1,2,3,4}", "详细接收", new Long(1), new Date(), 0);
        List <HouseEntity> list = houseService.list();
        for (HouseEntity houseEntity : list) {
            HouseEs houseEs = new HouseEs();
            BeanUtils.copyProperties(houseEntity, houseEs);
            houseESDao.save(houseEs);
        }

    }



}
